package com.guochenglang.www.service.impl;

import com.guochenglang.www.dao.InviteMessageDao;
import com.guochenglang.www.dao.MemberDao;
import com.guochenglang.www.dao.UserDao;
import com.guochenglang.www.pojo.InviteMessage;
import com.guochenglang.www.pojo.Member;
import com.guochenglang.www.pojo.User;
import com.guochenglang.www.service.InviteService;
import com.guochenglang.www.service.MemberService;
import com.guochenglang.www.service.UserService;
import com.guochenglang.www.service.anotate.Affair;
import com.guochenglang.www.util.MyDependency;

import java.sql.SQLException;
import java.util.ArrayList;

/**
 * 邀请业务
 *
 * @author GuoChenglang
 */
public class InviteServiceImpl implements InviteService {
    private static final UserDao USER_DAO = MyDependency.returnObject(UserDao.class);
    private static final InviteMessageDao INVITE_MESSAGE_DAO = MyDependency.returnObject(InviteMessageDao.class);
    private static final MemberDao MEMBER_DAO = MyDependency.returnObject(MemberDao.class);
    private static final UserService USER_SERVICE = MyDependency.returnObject(UserService.class);
    private static final MemberService MEMBER_SERVICE = MyDependency.returnObject(MemberService.class);

    /**
     * 邀请用户
     *
     * @param userId   邀请人用户id
     * @param baseId   知识库id
     * @param username 邀请的用户名
     * @return 主键
     */
    @Affair
    @Override
    public int addInvite(Integer userId, Integer baseId, String username) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //搜索是否有该用户名
        User isUser = new User();
        isUser.setUsername(username);
        ArrayList<User> select = USER_DAO.userSelect(isUser);
        //搜索是否已经邀请
        ArrayList<InviteMessage> select2 = INVITE_MESSAGE_DAO.inviteMessageSelect(new InviteMessage(null, username, null, baseId));
        Integer id = USER_DAO.userSelect(isUser).get(0).getId();
        //搜索是否已经是知识库成员
        ArrayList<Member> members = MEMBER_DAO.memberSelect(new Member(null, baseId, null, id));
        if (select.isEmpty() || !select2.isEmpty() || !members.isEmpty()) {
            return -1;
        }
        return INVITE_MESSAGE_DAO.insertInviteMessage(new InviteMessage(null, username, userId, baseId));
    }

    /**
     * 获得邀请信息个数
     *
     * @param username 用户名
     * @return 邀请信息个数
     */
    @Override
    public int getInviteCount(String username) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //封装条件
        InviteMessage condition = new InviteMessage();
        condition.setInviteUsername(username);
        return INVITE_MESSAGE_DAO.inviteMessageSelect(condition).size();
    }

    /**
     * 获得邀请信息列表
     *
     * @param username 用户名
     * @return 邀请信息列表
     */
    @Override
    public ArrayList<InviteMessage> showInviteMessages(String username) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        InviteMessage condition = new InviteMessage();
        condition.setInviteUsername(username);
        return INVITE_MESSAGE_DAO.inviteMessageSelect(condition);
    }

    /**
     * 删除邀请
     *
     * @param username 用户名
     * @param baseId   知识库id
     * @return 删除行数
     */
    @Affair
    @Override
    public int deleteInvite(String username, Integer baseId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //封装条件
        InviteMessage condition = new InviteMessage();
        condition.setInviteUsername(username);
        condition.setBaseId(baseId);
        return INVITE_MESSAGE_DAO.deleteInviteMessage(condition);
    }

    /**
     * 接受邀请
     *
     * @param baseId 知识库id
     * @param userId 用户id
     * @return 主键
     */
    @Affair
    @Override
    public int acceptService(Integer baseId, Integer userId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        InviteMessage condition = new InviteMessage();
        User conditionUser = new User();
        conditionUser.setId(userId);
        //搜索删除通知的条件
        ArrayList<User> users = USER_DAO.userSelect(conditionUser);
        condition.setInviteUsername(users.get(0).getUsername());
        condition.setBaseId(baseId);
        //删除该邀请
        INVITE_MESSAGE_DAO.deleteInviteMessage(condition);
        Member conditionMember = new Member();
        conditionMember.setMemberStatus("3");
        conditionMember.setMemberIde(userId);
        conditionMember.setBaseId(baseId);
        return MEMBER_DAO.insertMember(conditionMember);

    }
}
